package com.soho.mdi;

import java.io.File;
import java.io.FileFilter;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

public class BundleDetector {
	private BundleContext context = null;
	public BundleDetector(BundleContext context){
		this.context = context;
	}
	public void detect(File dir){	
		File[] files = dir.listFiles(new FileFilter(){
			public boolean accept(File file){
				return file.isFile()&&(file.getName().endsWith(".jar")||file.getName().endsWith(".zip"));
			}
		});
		
		Bundle[] bundles = context.getBundles();
		for (Bundle bundle:bundles){
			System.out.println(bundle.getBundleId()+","+bundle.getSymbolicName()+","+bundle.getState());
		}
		
		for (File file : files){
			//context.installBundle(file.get)
			try {
				context.installBundle("file:///"+file.getAbsolutePath());
			} catch (BundleException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(file.getAbsoluteFile());
		}
		
		bundles = context.getBundles();
		for (Bundle bundle:bundles){
			System.out.println(bundle.getBundleId()+","+bundle.getSymbolicName()+","+bundle.getState());
		}		
	}
}
